package com.zrrd.learn.chapter07字符串;

/**
 * 字符串习题03
 *
 * @author wrn
 * @version 1.0
 * @date 2023/7/18 13:13
 */

public class StringExercise03 {

    /**
     * "轻轻的我走了， 正如我轻轻的来；我轻轻的招手，作别西天的云彩。"最后一次出现"轻"的位置
     * @param str
     */
    public static void question01(String str,char cstr){

        System.out.println("第一题");
        int count = 0;
        for (int i = str.length()-1; i >= 0; i--) {
            char a = str.charAt(i);
            if (a==cstr){
                System.out.println("最后一次出现"+cstr+"的位置是"+i);
                break;
            }
        }
    }

    /**
     * 求字符串“powerful heaven dragon”中有多少种字符
     * @param str
     */
    public static void question02(String str){
        System.out.println("第二题");
        char[] astr = str.toCharArray();
    }

    /**
     * 字符串“peter piper picked a peck of pickled peppers”中有多少以p开头的单词
     * @param str
     */
    public static void question03(String str,char cstr){

        System.out.println("第三题");
        int count=0;
        if(str.charAt(0)==cstr){
            count++;
        }
        for (int i = 0; i < str.length(); i++) {
            if (str.charAt(i) == ' ') {
                if (str.charAt(i+1)==cstr){
                    count++;
                }
            }
        }
        System.out.println("字符串中有"+count+"个以"+cstr+"开头的单词");
    }


    public static void main(String[] args) {
        question01("轻轻的我走了, 正如我轻轻的来;我轻轻的招手,作别西天的云彩。",'轻');

        question02("powerful heaven dragon");

        question03("peter piper picked a peck of pickled peppers",'p');
    }

}
